package com.dk.controller;

import com.dk.pojo.TPerson;
import com.dk.pojo.TRole;
import com.dk.service.PersonService;
import com.dk.shiro.ShiroUtils;
import org.apache.dubbo.config.annotation.DubboReference;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.crypto.SecureRandomNumberGenerator;
import org.apache.shiro.crypto.hash.SimpleHash;
import org.apache.shiro.subject.Subject;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
@RequestMapping("person")
public class PersonController {

    @DubboReference
    private PersonService personService;

    @RequestMapping("toLogin")
    public String toLogin(){

        return "index";
    }

    @RequestMapping("getInfoById")
    @ResponseBody
    public Integer getInfoById(TPerson tPerson,String vcode,HttpServletRequest request) {

        TPerson t = personService.selectInfo(tPerson.getPname());

        //获取验证码
        String s = (String) request.getSession().getAttribute("code");

        if(!s.equalsIgnoreCase(vcode)){

            return 4;//验证码错误
        }

        //获取subject对象
        Subject subject = SecurityUtils.getSubject();
        //将传过来的账号密码封装成shiro的token对象
        UsernamePasswordToken token = new UsernamePasswordToken(tPerson.getPname(),tPerson.getPwd());

        try{

            subject.login(token);

        }catch (UnknownAccountException e){

            System.out.println("账号不存在");

            return 2;

        }catch (IncorrectCredentialsException e){

            System.out.println("密码错误");

            return 3;

        }
        return 1;
    }

    @RequestMapping("getInfo")
    @ResponseBody
    public List<TPerson> getInfo() {

        List<TPerson> list = personService.getInfo();

        return list;
    }

    @RequestMapping("toShow")
    public String toShow() {

        return "person/show";
    }

    @RequestMapping("deleteInfo")
    @ResponseBody
    public boolean deleteInfo(Integer pid) {

        return personService.deleteInfo(pid);
    }

    @RequestMapping("toAdd")
    public String toAdd(Model model){

        List<TRole> list = personService.toAdd();

        model.addAttribute("list",list);

        return "person/add";
    }
    @RequestMapping("insertInfo")
    @ResponseBody
    public boolean insertInfo(TPerson tPerson,Integer[] roleId){

        String salt = ShiroUtils.generateSalt(6);

        String password = ShiroUtils.encryptPassword("MD5", "1234", salt, 3);

        tPerson.setPwd(password);

        tPerson.setSalt(salt);

        boolean b = personService.insertInfo(tPerson,roleId);

        return true;
    }

    @RequestMapping("toUpdate")
    public String toUpdate(Model model, Integer pid) {

        List<TRole> list = personService.getRoleInfo();

        TPerson person = personService.selectInfoById(pid);

        List<Integer> ids = personService.getRoleById(pid);

        model.addAttribute("list", list);

        model.addAttribute("person", person);

        model.addAttribute("ids", ids);

        return "person/update";
    }
    @RequestMapping("updateInfoById")
    @ResponseBody
    public boolean updateInfoById(TPerson tPerson,Integer[] roleId){

        boolean b = personService.updateInfoById(tPerson,roleId);
        return true;
    }

    @RequestMapping("Unauthorized")
    public String Unauthorized(){

        return "auth";
    }
}
